package com.naizhuai.ui.task;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.naizhuai.Model.AccountHelper;
import com.naizhuai.Model.ModelProvider;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.Model.db.user.UserRepository;
import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.TaskActionApiStore;
import com.naizhuai.ui.BaseFragment;
import com.naizhuai.ui.adapter.SpacesItemDecoration;
import com.naizhuai.ui.entity.TaskListBean;
import com.naizhuai.ui.task.multtype.ImageBinderViewBinder;
import com.naizhuai.ui.task.multtype.TaskItem;
import com.naizhuai.ui.task.multtype.TaskItemViewBinder;
import com.naizhuai.ui.task.multtype.VitalityShow;
import com.naizhuai.ui.task.multtype.VitalityShowViewBinder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class TaskFragment extends BaseFragment {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    @BindView(R.id.containlist)
    RecyclerView containlist;
    private MultiTypeAdapter adapter;
    private Items mItems;
    private TaskActionApiStore mTaskActionApiStore;
    private PersonUserBean mPersonUserBean;
    public static TaskFragment newInstance() {
        TaskFragment fragment = new TaskFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_task, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        InitTaskData();
    }

    private void initView() {
        adapter = new MultiTypeAdapter();
        //首页轮播的逻辑
        adapter.register(String.class, new ImageBinderViewBinder());
        //当前活力展示
        adapter.register(VitalityShow.class, new VitalityShowViewBinder());
        //任务展示
        adapter.register(TaskItem.class, new TaskItemViewBinder());

        containlist.setLayoutManager(new LinearLayoutManager(getActivity()));

        mItems = new Items();
        adapter.setItems(mItems);
        containlist.setAdapter(adapter);

    }

    private void InitTaskData() {
        if (mTaskActionApiStore == null) {
            mTaskActionApiStore = TaskActionApiIml.getInstance();
        }
        //根据用户id 得到
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadData();
            }
        });
        refreshLayout.autoRefresh();

    }


    public void loadData() {
        mTaskActionApiStore.getTask_List(AccountHelper.getComPanyid())
                .subscribe(new NetWorkObserver<TaskListBean>() {
                    @Override
                    protected void onFail(String errormsg) {
                        if (refreshLayout!=null){
                            refreshLayout.finishRefresh();
                        }
                        shortToast(errormsg);
                    }

                    @Override
                    protected void onSuccess(TaskListBean result) {
                        if (refreshLayout!=null){
                            refreshLayout.finishRefresh();
                        }
                         initAdapter(result);
                    }
                });

    }


    private void initAdapter(TaskListBean bean) {
        mItems.clear();
        mItems.add(bean.bannerList.get(0).message);
        mItems.add(new VitalityShow(bean.vitality.message));
        for (TaskItem item:bean.getAllTaskList()){
             mItems.add(item);
        }
        adapter.notifyDataSetChanged();
//        mTaskAdapter = new TaskAdapter(bean.getAllTaskList());
//        mTaskAdapter.getOnGroupClick().subscribe(list ->
//                skipTaskDetial(list.getTaskId(), list.getTaskName(), list.getManagerInfo().getUsername(),
//                        list.getManagerInfo().getThumb())
//        );
//        task_expandview.setAdapter(mTaskAdapter);
//        int groupCount = task_expandview.getCount();
//        for (int i = 0; i < groupCount; i++)
//            task_expandview.expandGroup(i);
//
//        task_expandview.setOnGroupClickListener((parent, v, groupPosition, id) -> {
//            v.setClickable(true);
//            return true;
//        });
    }


    private void skipTaskDetial(String taskid, String taskname, String username, String thumb) {
        if (getActivity() != null) {
            Intent intent = new Intent(getActivity(), TaskDetialActivity.class);
            intent.putExtra(TaskDetialActivity.TASK_ID, taskid);
            getActivity().startActivity(intent);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
